from hanchlogger import logger
import openpyxl
import hanchxl


class BuMenDirectoryInfo(object):
    def __init__(self, name_yuan='', name_now='', is_all=True):
        self.nameYuan = name_yuan
        self.nameNow = name_now
        self.isAll = is_all


class ReplaceDirectory(object):
    def __init__(self, data_only=True):
        self.fileName = ''
        self.dataOnly = data_only
        self.bookDirectory = None
        self.sheetDirectory = None
        self.listDirectory = []

    def get_directory_info(self, sheet_src):
        maxRowSheet = sheet_src.max_row
        indexColNameCode = hanchxl.get_col_index(sheet_src, '序号')
        indexColNameYuan = hanchxl.get_col_index(sheet_src, '原部门名称')
        indexColNameNow = hanchxl.get_col_index(sheet_src, '现部门名称')
        indexColIsAll = hanchxl.get_col_index(sheet_src, '是否全词匹配')
        if indexColNameCode is not None and indexColNameYuan is not None and indexColNameNow is not None and \
                indexColIsAll is not None:
            for rowIndex in range(2, maxRowSheet + 1):
                directoryInfo = BuMenDirectoryInfo()
                valueXuHao = '{}'.format(sheet_src.cell(rowIndex, indexColNameCode).value).strip()
                if valueXuHao != '' and valueXuHao != 'None':
                    directoryInfo.nameYuan = '{}'.format(sheet_src.cell(rowIndex, indexColNameYuan).value).strip()
                    directoryInfo.nameNow = '{}'.format(sheet_src.cell(rowIndex, indexColNameNow).value).strip()
                    valueIsALl = '{}'.format(sheet_src.cell(rowIndex, indexColIsAll).value).strip()
                    logger.error('需替换部门 序号:{} 原部门名称:{} 现部门名称:{} 是否全词匹配:{}'.format(
                        valueXuHao, directoryInfo.nameYuan, directoryInfo.nameNow, valueIsALl))
                    if valueIsALl == '0':
                        directoryInfo.isAll = False
                    else:
                        directoryInfo.isAll = True
                    self.listDirectory.append(directoryInfo)

        else:
            logger.error('需替换部门 出错了: 序号:{} 原部门名称:{} 现部门名称:{} 是否全词匹配:{}'.format(indexColNameCode, indexColNameYuan, indexColNameNow, indexColIsAll))

    def load_directory(self, file_name):
        self.bookDirectory = openpyxl.load_workbook(file_name, data_only=self.dataOnly)
        self.sheetDirectory = self.bookDirectory.active
        maxRowDirectory = self.sheetDirectory.max_row
        maxColDirectory = self.sheetDirectory.max_column
        logger.debug('需替换部门 : {} 行 ， {} 列'.format(maxRowDirectory, maxColDirectory))
        self.get_directory_info(self.sheetDirectory)

    def get_replace_directory(self):
        return self.listDirectory

